package com.sunwayworld.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileUtils {

	public static String readFile(File fileName) {
		String fileContent = null;
		FileInputStream fis = null;
		FileChannel fc = null;
		try {
			fis = new FileInputStream(fileName);
			// get a file channel
			fc = fis.getChannel();

			// create a ByteBuffer that is large enough
			// and read the contents of the file into it
			// test
			// System.out.println(fc.size());
			ByteBuffer bb = ByteBuffer.allocate((int) fc.size() + 1);

			fc.read(bb);
			bb.flip();

			// save the content of the file as a String
			// if we want to change the encode
			// we can directly add a second parameter here
			// which is of course more efficent

			// System.out.println(bb.capacity());
			fileContent = new String(bb.array());

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// release the FileChannel
			try {
				fc.close();
			} catch (Exception ex) {

			}
			try {
				fis.close();
			} catch (Exception ex) {

			}
		}

		// write out the contents of this file
		return fileContent;

	}

}
